package io.cici.cc.mybatis.lite.binding;

import io.cici.cc.mybatis.lite.mapping.MappedStatement;
import io.cici.cc.mybatis.lite.mapping.SqlCommandType;
import io.cici.cc.mybatis.lite.session.Configuration;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

@Slf4j
@Setter
@Getter
public class SqlCommand {

    private final String id;
    private final SqlCommandType sqlCommandType;

    public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
        final String methodName = method.getName();
        MappedStatement mappedStatement = resolveMappedStatement(configuration, mapperInterface, methodName);
        id = mappedStatement.getId();
        sqlCommandType = mappedStatement.getSqlCommandType();
    }


    private MappedStatement resolveMappedStatement(Configuration configuration, Class<?> mapperInterface, String methodName) {
        String statementId = mapperInterface.getName() + "." + methodName;
        if (configuration.hasStatement(statementId)) {
            return configuration.getMappedStatement(statementId);
        }
        return null;
    }
}
